package com.carkeeper.order.task;

import com.carkeeper.order.dao.OrderMapper;
import com.carkeeper.order.pojo.Order;
import com.carkeeper.order.service.OutstandingService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import static org.springframework.context.i18n.LocaleContextHolder.setTimeZone;
@Api(tags = "自动关闭订单")
@Component
public class OrderClose {
    @Autowired
    private OutstandingService outstandingService;


    public Long getTime(){
        long time= System.currentTimeMillis();
        return time;
    }

    @Scheduled(cron = "0 0 0 * * ?")//每天0点执行一次
    @Transactional
    public void close(){
        List<Order> orderList = outstandingService.findByStatus(); //查询所有待接单的订单
        for (Order order : orderList) {
            Date createDate = order.getCreateDate();
            //数据库时间差8小时,减去8小时加24小时就是过期时间 大于等于过期时间 订单自动关闭
            if (getTime()>=createDate.getTime()-28800000+86400000){
               outstandingService.closeOrder(order.getId());
            }
        }
    }
}
